<template>
  <el-container>
    <el-header class="auth-header">
      <div class="back">
      <Back class="back-icon" @click="back" />
      <el-dialog v-model="dialogVisible" title="确认退出" @close="handleClose">
        <p>您确定要退出管理系统吗？</p>
      </el-dialog>
      </div>
      <div class="header-first">
      <h1>武术比赛报名系统</h1>
      </div>
      <div class="user-info">
            <el-card class="icon-card" @click="titleinformation">
              <EditPen class="header-icon" />
              比赛信息
            </el-card>
            <el-card class="icon-card" @click="fetchMatchGroup">
              <EditPen class="header-icon" />
              难度录入
            </el-card>
            <el-card class="icon-card" @click="handlePersonnelManage">
              <User class="header-icon" />
              人员管理
            </el-card>
            <el-card class="icon-card" @click="handleSingleSignup">
              <Tools class="header-icon" />
              团队管理
            </el-card>
            <el-card class="icon-card" @click="handleExportInfo">
              <Share class="header-icon" />
              导出信息
            </el-card>
      </div>
    </el-header>
    <RouterView></RouterView>
  </el-container>
</template>

<script setup>
import { ref, watch } from 'vue';
import { useRouter, RouterView, useRoute } from 'vue-router';
import { getMatches } from '../js/api';

const router = useRouter();
const route = useRoute();

const error = ref(''); // 错误信息
const matches = ref([]);
const isLoading = ref(false);
const competitions = ref([]); // 存储获取到的比赛数据
const Itemdisplay = ref(true); // 控制页面显示与隐藏
const dialogVisible = ref(false); // 控制退出显示与隐藏

const matchgroups = []; // 赛事id存储

const token = localStorage.getItem('adminToken'); // 从 localStorage 获取 token

// 跳转到比赛管理页面
function titleinformation() {
  router.push('/admin/adminchild');
}

// 难度申报切换页面
function fetchMatchGroup() {
  router.push('/admin/entergroup');
}

// 人员管理切换页面
function handlePersonnelManage() {
  router.push('/admin/allpetsonnel');
}


// 跳转导出数据打印
function handleExportInfo() {
  router.push('/admin/printpreview');
}


// 跳转团队管理页面
function handleSingleSignup() {
  router.push('/admin/allteamel');
}


// 退出
const back = () => {
  ElMessageBox.confirm('确认退出吗？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      // 用户点击确认
      localStorage.removeItem('adminToken');
      localStorage.removeItem('adminData');
      router.push('/login');
      // 这里可以执行退出的逻辑
    })
    .catch(() => {
      // 用户点击取消
    });
};


watch(() => route.path, (newPath, oldPath) => {
  // 当从 matches 路由切换到 admin 路由时
  if (newPath.includes('/admin')) {
    // 执行重新渲染的逻辑
    // console.log('从 matches 路由切换到 admin 路由，执行重新渲染逻辑');
    // 例如，你可以重置组件状态或重新获取数据
    async () => {
      try {
        const data = await getMatches();
        competitions.value = data.matches; // 将比赛列表赋值给 competitions
      } catch (err) {
        error.value = err.message;
      }
      for (let i = 0; i < matchId.value.length; i++) {
        matchgroups.push(matchId.value[i]);
      }
      fetchMatches(matchgroups);
    }
  }
});

function handleClose(){
  done(); // 调用 done() 来关闭对话框
}


</script>



<style scoped>
.auth-header {
  height: 150px;
  display: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #2c8fd5;
  color: rgb(255, 255, 255);
  /* line-height: 30px; */
}

.icon-card {
  margin-left: 10px;
  color: white;
  width: 115px;
  font-size: 18px;
  background-color: #8cbfde;
}

.user-info {
  display: flex;
  justify-content: space-between;
  flex-direction: row;

  font-size: 12px;
}
.back{
  margin-bottom: 2%;
  width: 100px;
  
}

h1{
  width: 100%;                      
  font-size: 36px;
}            
.header-first{
  display: flex;
  justify-content:flex-start;
}

svg {
  height: 50px;
  margin-top: 48px;
}

.header-icon {
  height: 50px;
  margin: 0 5px 0 0;
}

.shousuo {
  float: left;
  display: none;
}

.auth-main {
  width: 100%;
}
.end {
  margin-top: 10px;
}
</style>